package cn.t222.ch03.controller;

import cn.t222.ch03.mapper.BookMapper;
import cn.t222.ch03.pojo.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/books")
public class BookController {
    @Autowired
    private BookMapper bookMapper;
    List<Book> bookList = new ArrayList<>();

    @GetMapping("")
    public String books(Model model) {
        List<Book> bookList = bookMapper.getAll();
//        bookList.add(new Book(1, "三国","罗贯中", new Date()));
//        bookList.add(new Book(2, "红楼梦","曹雪芹", new Date()));
        model.addAttribute("bookList", bookList);
        return "book/books";
    }
    @GetMapping("/ftl")
    public String ftl(Model model) {
//        bookList.add(new Book(1, "三国","罗贯中", new Date()), new B);
//        bookList.add(new Book(2, "红楼梦","曹雪芹", new Date()));
//        model.addAttribute("bookList", bookList);
        return "books";
    }
    @GetMapping("/{id}")
    public String get(@PathVariable Integer id, Model model) {
        for(Book book: bookList) {
            if(book.getId().equals(id)) {
                model.addAttribute("book", book);
                break;
            }
        }
        return "book/detail";
    }
}
